home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / json / scanner.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  2KB  |  83 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Iterator based sre token scanner
  5.  
  6. '''
  7. import re
  8. import sre_parse
  9. import sre_compile
  10. import sre_constants
  11. from re import VERBOSE, MULTILINE, DOTALL
  12. from sre_constants import BRANCH, SUBPATTERN
  13. __all__ = [
  14.     'Scanner',
  15.     'pattern']
  16. FLAGS = VERBOSE | MULTILINE | DOTALL
  17.  
  18. class Scanner(object):
  19.     
  20.     def __init__(self, lexicon, flags = FLAGS):
  21.         self.actions = [
  22.             None]
  23.         s = sre_parse.Pattern()
  24.         s.flags = flags
  25.         p = []
  26.         for idx, token in enumerate(lexicon):
  27.             phrase = token.pattern
  28.             
  29.             try:
  30.                 subpattern = sre_parse.SubPattern(s, [
  31.                     (SUBPATTERN, (idx + 1, sre_parse.parse(phrase, flags)))])
  32.             except sre_constants.error:
  33.                 raise 
  34.  
  35.             p.append(subpattern)
  36.             self.actions.append(token)
  37.         
  38.         s.groups = len(p) + 1
  39.         p = sre_parse.SubPattern(s, [
  40.             (BRANCH, (None, p))])
  41.         self.scanner = sre_compile.compile(p)
  42.  
  43.     
  44.     def iterscan(self, string, idx = 0, context = None):
  45.         '''Yield match, end_idx for each match
  46.  
  47.         '''
  48.         match = self.scanner.scanner(string, idx).match
  49.         actions = self.actions
  50.         lastend = idx
  51.         end = len(string)
  52.         while True:
  53.             m = match()
  54.             if m is None:
  55.                 break
  56.             
  57.             (matchbegin, matchend) = m.span()
  58.             if lastend == matchend:
  59.                 break
  60.             
  61.             action = actions[m.lastindex]
  62.             if action is not None:
  63.                 (rval, next_pos) = action(m, context)
  64.                 if next_pos is not None and next_pos != matchend:
  65.                     matchend = next_pos
  66.                     match = self.scanner.scanner(string, matchend).match
  67.                 
  68.                 yield (rval, matchend)
  69.             
  70.             lastend = matchend
  71.  
  72.  
  73.  
  74. def pattern(pattern, flags = FLAGS):
  75.     
  76.     def decorator(fn):
  77.         fn.pattern = pattern
  78.         fn.regex = re.compile(pattern, flags)
  79.         return fn
  80.  
  81.     return decorator
  82.  
  83.